package com.sk.cardeal.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sk.cardeal.R;
import com.sk.cardeal.callback.MyCallBack;

/**
 * Created by sk on 2017/12/14.
 */

public class TitleView extends LinearLayout implements View.OnClickListener {

    private TextView tvTitle;
    private ImageView leftView;
    private ImageView rightView;
    private ImageView backView;

    private Context context;

    private String title;
    private int leftId, rightId;

    private MyCallBack myCallBack;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        tvTitle.setText(title);
    }

    public int getLeftId() {
        return leftId;
    }

    public void setLeftId(int leftId) {
        this.leftId = leftId;
    }

    public int getRightId() {
        return rightId;
    }

    public void setRightId(int rightId) {
        this.rightId = rightId;
    }

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.layout_title_view, this);

        tvTitle = findViewById(R.id.tv_title);
        leftView = findViewById(R.id.left_view);
        rightView = findViewById(R.id.right_view);
        backView = findViewById(R.id.back);

        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TitleView, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.TitleView_leftView:
                    leftId = array.getResourceId(attr, 1);
                    break;
                case R.styleable.TitleView_rightView:
                    rightId = array.getResourceId(attr, 1);
                    break;
                case R.styleable.TitleView_title:
                    title = array.getString(attr);
                    break;
            }
        }

        tvTitle.setText(title);
        if (leftId != 0) {
            leftView.setImageResource(leftId);
        }
        if (rightId != 0) {
            rightView.setImageResource(rightId);
        }

        backView.setOnClickListener(this);
        leftView.setOnClickListener(this);
        rightView.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                if(myCallBack!=null){
                    myCallBack.clickTitle(1);
                }
                break;
            case R.id.left_view:
                if(myCallBack!=null){
                    myCallBack.clickTitle(2);
                }
                break;
            case R.id.right_view:
                if(myCallBack!=null){
                    myCallBack.clickTitle(3);
                }
                break;
        }
    }
}
